import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    userId: null,
    userName: '',
    avatar: '',
    token: '',
    role: '',
    points: 0,
    level: 1,
    isLogin: false
  }),
  
  actions: {
    setUserInfo(userInfo) {
      this.userId = userInfo.userId || userInfo.id;
      this.userName = userInfo.userName || userInfo.username;
      this.avatar = userInfo.avatar || '';
      this.token = userInfo.token || '';
      this.role = userInfo.role || '';
      this.points = userInfo.points || 0;
      this.level = userInfo.level || 1;
      this.isLogin = true;
    },
    
    logout() {
      this.userId = null;
      this.userName = '';
      this.avatar = '';
      this.token = '';
      this.role = '';
      this.points = 0;
      this.level = 1;
      this.isLogin = false;
    },
    
    updatePoints(newPoints) {
      this.points = newPoints;
    },
    
    clearUserInfo() {
      this.logout();
    }
  }
});